package me.zingon.WebSocket.model;

import me.zingon.WebSocket.tools.Players;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import java.util.Random;

/**
 * Created by ZTC on 12/14/2016.
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Tank extends BaseModel {

    @Autowired
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    Players players;

    String id;

    int bulletSum;

    int bulletNum;

    int bulletSpeed;

    boolean isMoveRunning;

    String action;


    @Autowired
    public Tank(@Value("${tank.speed}") int speed,
                @Value("${bullet.speed}") int bulletSpeed,
                @Value("${tank.bulletNum}") int bulletNum,
                @Value("${tank.width}") int width,
                @Value("${tank.height}") int height,
                @Value("${screen.x}") int screenX,
                @Value("${screen.y}") int screenY) {
        this.speed = speed;
        this.bulletSpeed = bulletSpeed;
        this.bulletNum = bulletNum;
        this.width = width;
        this.height = height;
        this.direct = "W";
        this.bulletSum = 0;
        this.isAlive = true;
        Random rand = new Random();
        this.x = rand.nextInt(screenX - 60) + 5;
        this.y = rand.nextInt(screenY - 60) + 5;
        this.screenX = screenX;
        this.screenY = screenY;
        this.isMoveRunning = false;
    }

    public void move(final String action) {
        setAction(action);
        if (isMoveRunning)
            return;
        if (!isMoveRunning) {
            new Thread(new Runnable() {
                public void run() {
                    while (isAlive) {
                        if (getAction().contains("W"))
                            up();
                        if (getAction().contains("S"))
                            down();
                        if (getAction().contains("A"))
                            left();
                        if (getAction().contains("D"))
                            right();
                        if (getAction().contains("J")) {
                            Bullet b = shot();
                            if (b != null)
                                players.putBullet(b);
                        }
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
            isMoveRunning = true;
        }
    }

    public void up() {
        if (this.y - speed > 0) {
            this.y -= speed;
        }
        this.direct = "W";
    }

    public void down() {
        if (this.y + speed + height < screenY) {
            this.y += speed;
        }
        this.direct = "S";
    }

    public void left() {
        if (this.x - speed > 0) {
            this.x -= speed;
        }
        this.direct = "A";
    }

    public void right() {
        if (this.x + speed + width < screenX) {
            this.x += speed;
        }
        this.direct = "D";
    }

    public Bullet shot() {
        if (bulletNum <= 0)
            return null;
        WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
        Bullet b = (Bullet) wac.getBean("bullet");
        b.setDirect(direct);
        b.setOwer(id);
        b.setId(bulletSum);
        b.setSpeed(bulletSpeed);
        bulletNum--;
        bulletSum++;
        if (direct.equals("W")) {
            b.setX(x + 20);
            b.setY(y - 10);
        } else if (direct.equals("D")) {
            b.setX(x + 50);
            b.setY(y + 20);
        } else if (direct.equals("A")) {
            b.setX(x - 10);
            b.setY(y + 20);
        } else if (direct.equals("S")) {
            b.setX(x + 20);
            b.setY(y + 50);
        }
        //threadPoolTaskExecutor.execute(b);
        //new Thread(b).start();
        return b;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getBulletSum() {
        return bulletSum;
    }

    public void setBulletSum(int bulletSum) {
        this.bulletSum = bulletSum;
    }

    public int getBulletNum() {
        return bulletNum;
    }

    public void setBulletNum(int bulletNum) {
        this.bulletNum = bulletNum;
    }

    public int getBulletSpeed() {
        return bulletSpeed;
    }

    public void setBulletSpeed(int bulletSpeed) {
        this.bulletSpeed = bulletSpeed;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }
}
